<!-- 模板，div ，el ui元素布局-->
<template>
  <!-- 一个根DIV -->
  <div>
    <!-- 1.导航条 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>博客管理</el-breadcrumb-item>
      <el-breadcrumb-item>博客列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 2.卡片视图 -->
    <el-card>
      <!-- 2.1 顶部操作  el-row/el-col-->
      <el-form :inline="true"  label-width="40px" style="margin-bottom: -15px">
        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="标题">
              <el-input clearable
                v-model="queryInfo.title"
                placeholder="请输入标题"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="摘要">
              <el-input clearable
                v-model="queryInfo.description"
                placeholder="请输入摘要"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="分类">
             
             <el-cascader
                expand-trigger="hover"
                :options="parentCateList"
                :props="cascaderProps"
                v-model="selectedKeys"
                @change="parentCateChanged"
                clearable
                change-on-select
              >
              </el-cascader>


            </el-form-item>
          </el-col>



       
          <el-col :span="6">
            <el-form-item>
            <el-button type="primary" @click="getList">搜索</el-button>
           
            <el-button type="primary" @click="removeById(multipleSelection)">删除</el-button>
            </el-form-item>  
          </el-col>
        </el-row>
      </el-form>

      <!-- 2.2 列表区域 -->
      <el-table
        :data="list"
        border
        stripe
        @selection-change="handleSelectionChange"
      >
        <!-- 索引列 -->

        <el-table-column type="selection" width="60"></el-table-column>
        <el-table-column type="index" label="序号" width="60"></el-table-column>
        <el-table-column label="标题" prop="title"></el-table-column>
        <el-table-column label="摘要" prop="description"></el-table-column>
        <el-table-column label="发表时间" prop="created"></el-table-column>

         <el-table-column label="博客分类">
             <template slot-scope="scope">
               {{scope.row.centity.cname}}
             </template>
         </el-table-column>
        <!-- 自定义列 -->
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="showEditDialog(scope.row.id)"
              >编辑
            </el-button>
           

           <el-button
              size="mini"
              type="primary"
              icon="el-icon-document"
              @click="showInfoById(scope.row.id)"
              >详情
            </el-button>

            <!-- {{scope.row}} -->
          </template>
        </el-table-column>
      </el-table>

      <!-- 2.3 分页区域 -->

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-size="queryInfo.pagesize"
        :page-sizes="[5, 10, 20, 40]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 3.弹框区域 -->

    <!-- 添加框 -->
    <el-dialog
      title="添加"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogClosed"
    >
      <el-form
        :model="addForm"
        ref="addFormRef"
        :rules="addFormRules"
        label-width="100px"
      >
        <el-form-item label="标题" prop="title">
          <el-input v-model="addForm.title"></el-input>
        </el-form-item>
        <el-form-item label="摘要" prop="description">
          <el-input v-model="addForm.description"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input type="textarea" v-model="addForm.content"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑框 -->
    <el-dialog
      title="编辑角色"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClosed"
    >
      <el-form
        :model="editForm"
        ref="editFormRef"
        :rules="editFormRules"
        label-width="100px"
      >
        <el-form-item label="标题" prop="title">
          <el-input v-model="editForm.title"></el-input>
        </el-form-item>
        <el-form-item label="摘要" prop="description">
          <el-input v-model="editForm.description"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input type="textarea" v-model="editForm.content"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </span>
    </el-dialog>

  <!-- 详情抽屉 -->
    <el-drawer
      title="博客详情"
      :visible.sync="showDrawer"
      direction="rtl"
      size="50%"
    >
      <div style="padding: 5px;margin-top:-30px;">
          <h1 v-html="info.title" style="text-align:center"></h1>
          <h3 v-html="info.description" style="text-align:center"></h3> 
          <h5 v-html="info.created" style="text-align:center"></h5>   
          <div style="width:80%;margin:5px auto;border:1px solid blue">
            <img :src="$store.state.baseURL + info.img" style="height:100px"/>
            </div>
             <div style="width:80%;margin:5px auto;border:1px solid blue">
            <img v-for="(item,index) in info.blogmulimglist" :src="$store.state.baseURL + item.imgpath" style="height:100px" :key="index"/>
            </div>
          <div v-html="info.content" style="width:80%;margin:5px auto;border:1px solid blue"/>        
      </div>
      <!-- 底部 -->
      <div style="text-align: center">
        <el-button @click="showDrawer=false">关 闭</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<!-- 行为，业务逻辑 -->
<script>
export default {
  //变量，配合ElementUI使用
  data() {
    return {

 //分类相关
      parentCateList: [], //后台传来的树形结构数据
      cascaderProps: {
        //配置怎么解析后台返来的数据
        label: "cname",
        value: "id",
        children: "children",
      },
      selectedKeys: "", //路径


      //抽屉
      showDrawer:false,
      info:{},
      /// ////////////////////列表 el-table
      list: [],

      multipleSelection: [], //多选  id

      /// ////////////////////分页查询   list->page
      queryInfo: {
        title: "", //查询参数
        description: "",
        cId:"",    //分类id
        pagenum: 1, // 当前的页数
        pagesize: 10, // 当前每页显示多少条数据
      },
      total: 0,

      /// ////////////////////添加框  form，验证
      addDialogVisible: false,
      addForm: {
        title: "",
        description: "",
        content: "",
      },
      addFormRules: {
        title: [
          { required: true, message: "请输入标题", trigger: "blur" },
          {
            min: 3,
            max: 25,
            message: "长度在 3 到 25 个字符",
            trigger: "blur",
          },
        ],
        description: [
          { required: true, message: "请输入摘要", trigger: "blur" },
        ],
        content: [{ required: true, message: "请输入内容", trigger: "blur" }],
      },

      /// ////////////////////修改框  form，验证
      editForm: {},
      editDialogVisible: false,
      editFormRules: {
        title: [
          { required: true, message: "请输入标题", trigger: "blur" },
          {
            min: 3,
            max: 25,
            message: "长度在 3 到 25 个字符",
            trigger: "blur",
          },
        ],
        description: [
          { required: true, message: "请输入摘要", trigger: "blur" },
        ],
        content: [{ required: true, message: "请输入内容", trigger: "blur" }],
      },
    };
  },
  //页面初始化加载，生命周期函数
  created() {
    this.getList();

     //获取数据
    this.getCateList();
  },
  //自定义函数，操作变量
  methods: {

     // 获取分类数据
    async getCateList() {
      const { data: res } = await this.$http.post("/category/list");
      if (res.code !== 200) {
        return this.$message.error("获取分类失败！"+res.msg);
      }
      // 把数据列表，赋值给
      this.parentCateList = res.data;
      this.$gs.getTreeData(this.parentCateList)   //去除空children
    },

    //分类选择触发
    parentCateChanged() {
      console.log("---", this.selectedKeys);
      this.queryInfo.cId = this.selectedKeys[this.selectedKeys.length - 1];
    },


    // 多选,组成id数组
    handleSelectionChange(val) {
      console.log(val);
      //复制id到multipleSelection   map  [3,2,5,6,7]
      this.multipleSelection = val.map((x) => {
        return x.id;
      });
      console.log("选择的id：", this.multipleSelection);
    },

    /// //////////////////// 获取所有角色的列表
    async getList() {
      //query=abc&pagenum=1&pagesize=5
      console.log(this.$qs.stringify(this.queryInfo));
      const { data: res } = await this.$http.post(
        "/blog/list",
        this.$qs.stringify(this.queryInfo)
      );
      console.log(res);
      if (res.code !== 200) {
        return this.$message.error("获取列表失败！"+res.msg);
      }
      this.list = res.data.records;
      this.total = res.data.total;
    },

    /// ////////////////////分页方法
    // 监听 pagesize 改变的事件
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.queryInfo.pagesize = val;
      this.getList();
    },

    // 监听 页码值 改变的事件
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.queryInfo.pagenum = val; // 获取新页码
      this.getList();
    },

    /// ////////////////////添加功能
    showAddDialog() {
      this.addDialogVisible = true;
    },
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    add() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        // 可以发起添加用户的网络请求
        const { data: res } = await this.$http.post("/blog/save", this.addForm);

        if (res.code !== 200) {
          return this.$message.error("添加信息失败！"+res.msg);
        }

        this.$message.success("添加信息成功！");
        this.addDialogVisible = false;
        this.getList();
      });
    },



    //////////////////////////详情
   async  showInfoById(id){
      console.log(id);
      const { data: res } = await this.$http.get("/blog/info/" + id);
      if (res.code !== 200) {
        return this.$message.error("查询信息失败！"+res.msg);
      }
      console.log(res.data);
      this.info=res.data;
      this.showDrawer=true
    },



    /// ////////////////////编辑功能
    async showEditDialog(id) {
      console.log(id);
      const { data: res } = await this.$http.get("/blog/info/" + id);
      if (res.code !== 200) {
        return this.$message.error("查询信息失败！"+res.msg);
      }
      console.log(res.data);
      this.editForm = res.data; // 对象复制
      this.editDialogVisible = true;
    },
    editDialogClosed() {
      this.$refs.editFormRef.resetFields();
    },
    update() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return;
        //
        const { data: res } = await this.$http.post(
          "/blog/update",
          this.editForm
        );
        if (res.code !== 200) {
          return this.$message.error("更新信息失败！"+res.msg);
        }

        // 提示修改成功
        this.$message.success("更新信息成功！");
        // 关闭对话框
        this.editDialogVisible = false;
        // 刷新数据列表
        this.getList();
      });
    },

    /// ////////////////////删除功能   ，实现批量删除
    removeById(ids) {
      console.log("-----", ids);
      if (ids.length == 0) return this.$message.error("请先选择至少一条");

      // 弹框
      this.$confirm("此操作将永久删除数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          // 删除逻辑
          const { data: res } = await this.$http.post("/blog/delete", ids);

          if (res.code !== 200) {
            return this.$message.error("删除信息失败！"+res.msg);
          }

          this.$message.success("删除信息成功！");

          this.getList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<!-- css,样式 ，scoped 本页CSS有效，生命同名class <el-card> ==> <div class="el-card">  -->
<style  scoped>
</style>
